package com.das.mechanic_base.widget;

import android.app.Dialog;
import android.content.Context;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;

import androidx.recyclerview.widget.LinearLayoutManager;

import com.das.mechanic_base.R;
import com.das.mechanic_base.adapter.record.X3DateAdapter;
import com.das.mechanic_base.bean.main.HomeColorBean;
import com.das.mechanic_base.utils.X3ScreenUtils;
import com.das.mechanic_base.utils.X3StringUtils;
import com.umeng.analytics.MobclickAgent;

import java.util.List;

public class X3DateDialog {

    private View view;
    private X3MaxHeightRecyclerView rlv_area;
    private X3DateAdapter dateAdapter;
    private Dialog dialog;

    public void showOtherDialog(Context context, View clickView) {
        //1、使用Dialog、设置style
        dialog = new Dialog(context, R.style.pointDialog);
        //2、设置布局
        view = View.inflate(context, R.layout.x3_date_item, null);
        dialog.setContentView(view);

        rlv_area = view.findViewById(R.id.rlv_area);
        rlv_area.setLayoutManager(new LinearLayoutManager(context));
        dateAdapter = new X3DateAdapter(context);
        rlv_area.setAdapter(dateAdapter);
        dateAdapter.setiOnClickSelect(colorBean -> {
            // 选择筛选条件
            if (dialog != null) {
                dialog.dismiss();
            }
            if (iOnClickSelect != null) {
                iOnClickSelect.iOnSelectDate(colorBean);
            }
        });
        dialog.setOnDismissListener(dialog -> MobclickAgent.onPageEnd("选择接车单日期弹窗"));

        Window window = dialog.getWindow();
        window.setGravity(Gravity.LEFT | Gravity.TOP);

        clickView.post(() -> {
            WindowManager.LayoutParams layoutParams = window.getAttributes();
            layoutParams.width = ViewGroup.LayoutParams.WRAP_CONTENT;
            layoutParams.height = ViewGroup.LayoutParams.WRAP_CONTENT;
            layoutParams.x = X3ScreenUtils.dipToPx(15,context);
            layoutParams.y = (int) (clickView.getY() + clickView.getHeight() / 2);
            window.setAttributes(layoutParams);
        });

    }

    IOnClickSelect iOnClickSelect;

    public void setiOnClickSelect(IOnClickSelect iOnClickSelect) {
        this.iOnClickSelect = iOnClickSelect;
    }

    public interface IOnClickSelect{
        void iOnSelectDate(HomeColorBean colorBean);
    }

    /**
     * 暂时区号
     * @param mList
     */
    public void changeArea(List<HomeColorBean> mList) {
        if (!X3StringUtils.isListEmpty(mList) && dateAdapter != null) {
            dateAdapter.changeData(mList);
            if (dialog != null) {
                dialog.show();
                MobclickAgent.onPageStart("选择接车单日期弹窗");
            }
        }
    }
}
